package com.smart.buss.ctrl;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.smart.buss.service.*;
import com.smart.buss.vo.*;
import com.smart.primary.common.util.*;
import com.smart.primary.util.HideDataUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.WebUtils;

import com.alibaba.fastjson.JSONObject;
import com.smart.buss.vo.resp.BussUserResp;
import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.common.Constants;
import com.smart.primary.sys.service.UserService;
import com.smart.primary.sys.vo.User;
import com.smart.primary.util.SessionUtil;

@RestController
@RequestMapping("/buss/user")
public class BussUserController extends BaseCtrl {
	private final static Logger logger= Logger.getLogger(BussUserController.class);

	@Autowired
	private BussUserService bussUserService;
	@Autowired
	private UserService sysUserService;
	@Autowired
	private BussDakaService bussDakaService;
	@Autowired
	private BussDakaLogService bussDakaLogService;
	@Autowired
	private BussAccountLogService bussAccountLogService;
	@Autowired
	private BussTaskService bussTaskService;
	@Autowired
	private BussTaskLogService bussTaskLogService;

	@RequestMapping
	public AjaxResult getList(BussUser entity) throws Exception{
		List<BussUser> list=bussUserService.queryListByPage(entity);
		String path=PropertiesUtil.getString("res.web.url");
		for(BussUser user:list ){
			if(StringUtils.isNotBlank(user.getTwoCode())){
				user.setTwoCode(path+user.getTwoCode());
			}
		}
		return getResult(entity, list);
	}
	//http://192.168.0.5:8080/task/buss/user/info?phone=15036760098
	@RequestMapping("/info")
	public AjaxResult userInfo(BussUser entity) throws Exception{
		String phone = getUserMobile();
		try {
			BussUserResp resp = null;
			if(StringUtils.isNotBlank(phone)){
				entity.setId(phone);
				resp=bussUserService.info(entity);

				String path=PropertiesUtil.getString("res.web.url");
				String herf=PropertiesUtil.getString("res.code.url");
				if(StringUtils.isNotBlank(resp.getTwoCode())){
					resp.setTwoCode(path+resp.getTwoCode());
				}

				resp.setTwoCodeHerf(herf+resp.getCode());
				if(StringUtils.isNotBlank(resp.getPhone())){
					resp.setName(HideDataUtil.hidePhoneNo(resp.getPhone()));
				}else {
					resp.setName(InviteCode.getName(8));
				}

				resp.setDaka(getDake(phone));

				resp.setDakaVip(getDakeVip(phone));

				resp.setTixian(getTixian(phone));
			}else {
				resp = new BussUserResp();
				resp.setAccount(0D);
			}
			
			return AjaxResult.success(resp);
		}catch (Exception e){
			e.printStackTrace();
			return AjaxResult.failure("参数有误");
		}

	}

	@RequestMapping("list")
	public List<BussUser> list(BussUser entity) throws Exception{
		return bussUserService.queryList(entity);
	}
	//http://localhost:8080/task/buss/user/save?fCode=111111&phone=12345678910
	@RequestMapping("/save")
	public AjaxResult save(BussUser entity) throws Exception{
		if(StringUtils.isBlank(entity.getPhone()) || StringUtils.isBlank(entity.getCode())){
			return AjaxResult.failure("参数不足");
		}

		return AjaxResult.updateSuccess();
	}
	@RequestMapping("/update")
	public AjaxResult update(BussUser entity) throws Exception{
		String phone = getUserMobile();
		if(StringUtils.isNotBlank(phone)){
			entity.setId(phone);
		}
		BussUser user=new BussUser();
		user.setId(entity.getId());
		BussUser query=bussUserService.queryObject(user);
		if(query==null){

			return AjaxResult.failure("参数错误");
		}

		bussUserService.update(entity);
		return AjaxResult.updateSuccess();
	}



	@RequestMapping("/uppwd")
	public AjaxResult uppwd(User entity,String newPwd) throws Exception{
		User sysUser = (User) WebUtils.getSessionAttribute(request, Constants.SESSION_USER);
		if(sysUser != null){
			entity.setUserId(sysUser.getUserId());
		}else {
			return AjaxResult.failure("未登录");
		}
		entity.setUserPwd(MD5Util.getMD5Code(entity.getUserPwd()));
		User user = this.sysUserService.queryObject(entity);
		if (user == null) {
			return AjaxResult.failure("原密码错误");
		}else {
			entity.setUserPwd(MD5Util.getMD5Code(newPwd));
		}
		sysUserService.update(entity);
		return AjaxResult.deleteSuccess();
	}

	@RequestMapping("/del")
	public AjaxResult del(String id) throws Exception{
		bussUserService.delete(id);
		return AjaxResult.deleteSuccess();
	}


	public Boolean getDakeVip(String phone) throws Exception {
		Boolean bool=false;
		if(StringUtils.isBlank(phone)){
			return bool;
		}
		BussTask bussTask=new BussTask();
		bussTask.setIshide(1);
		bussTask=bussTaskService.queryObject(bussTask);
		if(bussTask!=null){
			BussTaskLog bussTasklog=new BussTaskLog();
			bussTasklog.setTaskId(bussTask.getId());
			bussTasklog.setUserId(phone);
			bussTasklog.setCheckStatus(1);
			try {
				bussTasklog=bussTaskLogService.queryObject(bussTasklog);
				if(bussTasklog==null){
					bool=true;
				}
			}catch (Exception e){}
		}
		return bool;
	}

	Boolean getDake(String phone) throws Exception {
		Boolean bool=false;
		if(StringUtils.isBlank(phone)){
			return bool;
		}
		BussDaka bdaka=new BussDaka();
		bdaka.setStatus(0);
		BussDaka querydaka=bussDakaService.queryObject(bdaka);
		if(querydaka!=null || StringUtils.isNotBlank(querydaka.getIssueNumber())){
			BussDakaLog log=new BussDakaLog();
			log.setDakaId(querydaka.getIssueNumber());
			log.setUserId(phone);
			BussDakaLog query=bussDakaLogService.queryObject(log);
			if(query!=null){
				bool=true;
			}
		}
		return bool;
	}


	Integer getTixian(String phone) throws Exception {
		BussAccountLog account=new BussAccountLog();
		account.setUserId(phone);
		account.setLogtype(-1);
		return bussAccountLogService.queryCount(account);
	}

}
